package com.satan.juc.demo16;

import java.util.concurrent.CopyOnWriteArrayList;

public class ProductList {
    private CopyOnWriteArrayList<String> productList;

    public ProductList() {
        productList = new CopyOnWriteArrayList<>();
    }

    // 添加产品
    public void addProduct(String product) {
        productList.add(product);
    }

    // 获取产品
    public String getProduct(int index) {
        return productList.get(index);
    }

    public static void main(String[] args) {
        ProductList list = new ProductList();

        // 模拟多线程环境下的添加产品
        new Thread(() -> {
            list.addProduct("Product 1");
            System.out.println("Added Product 1");
        }).start();

        new Thread(() -> {
            list.addProduct("Product 2");
            System.out.println("Added Product 2");
        }).start();

        // 模拟多线程环境下的获取产品
        new Thread(() -> {
            System.out.println("Product at index 0: " + list.getProduct(0));
        }).start();
    }
}
